<?php
namespace app\dev\controller;
use app\system\controller\SystemController;
/**
 * 脚手架
 */
class ScaffoldController extends SystemController {

    /**
     * 当前模块参数
     */
	protected function _infoModule(){
		return array(
            'info' => array(
                    'name' => '脚手架',
                    'description' => '系统模块生成助手',
                ),
            'menu' => array(
                    array(
                        'name' => '模块',
                        'icon' => 'info',
                        'url'  => url('index'),
                    ),
                    array(
                        'name' => '控制器',
                        'icon' => 'cogs',
                        'url'  => url('appController'),
                    ),
                    array(
                        'name' => '模型',
                        'icon' => 'database',
                        'url'  => url('appModel'),
                    ),
                    array(
                        'name' => '视图',
                        'icon' => 'eye',
                        'url'  => url('appView'),
                    ),
                )
            );
	}
	/**
     * 列表
     */
    public function index(){
        if(!IS_POST){
            $this->systemDisplay();
        }else{
            $app = request('post.app');
            $name = request('post.name');
            $system = request('post.system');
            if(empty($app) || empty($name)) {
                $this->error('信息填写不完整！');
            }
            //建立文件夹
            $dir = array( $app, $app.'/conf', $app.'/controller', $app.'/model', $app.'/service', $app.'/view');
            foreach ($dir as $value) {
                if(!mkdir(BASE_PATH . 'app/' . $value, '0777', true)){
                    $this->error('无文件写入权限，请检查！');
                }
            }
            //写入配置
            $data = "<?php
return array(
    'APP_SYSTEM' => {$system},
    'APP_STATUS' => 1,
    'APP_INSTALL' => 1,
    'APP_NAME' => '{$name}',
    );";
            file_put_contents(BASE_PATH . 'app/'.$app.'/conf/config.php', $data);
            //生成菜单文件
            $data = "<?php
namespace app\\{$app}\\service;
/**
 * 系统菜单接口
 */
class MenuService{
    /**
     * 获取菜单结构
     */
    public function getSystemMenu(){
        return array(
            '{$app}' => array(
                'name' => '{$name}',
                'icon' => 'home',
                'order' => 0,
                'menu' => array(
                    array(
                        'name' => '{$name}首页',
                        'icon' => 'home',
                        'url' => url('system/Index/index'),
                        'order' => 0
                    )
                )
            ),
        );
    }
}

";
            file_put_contents(BASE_PATH . 'app/'.$app.'/service/MenuService.php', $data);

            //生成权限文件
            $data = "<?php
namespace app\\{$app}\\service;
/**
 * 权限接口
 */
class PurviewService{
    /**
     * 获取模块权限
     */
    public function getSystemPurview(){
        return array();
    }
}

";
            file_put_contents(BASE_PATH . 'app/'.$app.'/service/PurviewService.php', $data);
        }
    }

    /**
     * 控制器
     */
    public function appController(){
        $this->systemDisplay();
    }

    /**
     * 模型
     */
    public function appModel(){
        $this->systemDisplay();
    }

    /**
     * 视图
     */
    public function appView(){
        $this->systemDisplay();
    }

    /**
     * 被动接口
     */
    public function appServer(){
        $this->systemDisplay();
    }
    
}

